package com.example.minefieldkeepercloud.view.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.util.DensityUtil;

import java.util.List;



/**
 * 统计表和统计图选择
 *
 * @Author:Created by  ygy
 * @time:2019/10/10
 */
public class TypeSelectPopupWindow extends PopupWindow {

    private Activity context;
    private View view;
    private int kind;

    private OnSelectLayoutListener onSelectLayoutListener;




    public TypeSelectPopupWindow(int kind,Context context, OnSelectLayoutListener onSelectLayoutListener) {
        super(context);
        this.context = (Activity) context;
        this.kind=kind;
        this.onSelectLayoutListener = onSelectLayoutListener;
        initPop();

        initView();

    }

    private void initView() {


        final TextView tv_table=view.findViewById(R.id.tv_table);
        final TextView tv_chart=view.findViewById(R.id.tv_chart);
        if(kind==1)
        {
            tv_chart.setText("统计图");
            tv_table.setText("统计表");
        }
        else
        {
            tv_chart.setText("待处理");
            tv_table.setText("已处理");
        }
        tv_table.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_table.setTextColor(context.getResources().getColor(R.color.system_color));
                tv_chart.setTextColor(context.getResources().getColor(R.color.black3));
                onSelectLayoutListener.selectTypeLayout(1);
                dismiss();
            }
        });

        tv_chart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_chart.setTextColor(context.getResources().getColor(R.color.system_color));
                tv_table.setTextColor(context.getResources().getColor(R.color.black3));
                onSelectLayoutListener.selectTypeLayout(0);
                dismiss();
            }
        });

    }


    /**
     * 初始化pop样式
     */
    private void initPop() {

        view = LayoutInflater.from(context).inflate(R.layout.pop_type_select_layout, null);
        int width = context.getWindowManager().getDefaultDisplay().getWidth();
        ;
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);

        this.setHeight(DensityUtil.dip2px(context,90));
        this.setContentView(view);


        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.gray_bg2));
        //设置SelectPicPopupWindow弹出窗体的背景
        setBackgroundDrawable(dw);

        //获取popwindow焦点
        setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        setOutsideTouchable(true);

        //设置动画
        setAnimationStyle(R.style.AnimationPreview);

//        backgroundAlpha(0.6f);


        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }

        });
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        super.showAsDropDown(anchor, xoff, yoff);
        backgroundAlpha(0.6f);
    }

    /**
     * \
     * 设置透明背景
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }


    /**
     * 类型选择回调C
     */
    public interface OnSelectLayoutListener {
        public void selectTypeLayout(int i);//不限

    }
}